package com.atguigu.demojuc.chap02;

//演示死锁情况


public class DeadlockDemo {

    public static void main(String[] args) {
        //声明两把锁
        Object obj1 = new Object();
        Object obj2 = new Object();

        //声明两个线程
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t" + "线程准备获取锁1");
            synchronized (obj1) {
                System.out.println(Thread.currentThread().getName() + "\t" + "获得锁1" + "准备获取锁2");
                synchronized (obj2) {
                    System.out.println(Thread.currentThread().getName() + "\t" + "获得锁2");
                }
            }
        }, "A").start();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t" + "线程准备获取锁2");
            synchronized (obj2) {
                System.out.println(Thread.currentThread().getName() + "\t" + "获得锁2"  + "准备获得锁1");
                synchronized (obj1) {
                    System.out.println(Thread.currentThread().getName() + "\t" + "获得锁1");
                }
            }

        }, "B").start();
    }

}
